package com.example.weather.Activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.weather.Adapter.DeletecityAdapyer;
import com.example.weather.DB.Dbmanager;
import com.example.weather.R;

import java.util.ArrayList;
import java.util.List;

public class DeleteCityActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView delete_iv_error,delete_iv_right;
    private ListView delete_lv;
    private List<String> mdata;//listview的数据源
    private List<String> dcity;//删除的城市的数据源
    private DeletecityAdapyer deletecityAdapyer;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_city);
        delete_iv_error=findViewById(R.id.delete_iv_error);
        delete_iv_right=findViewById(R.id.delete_iv_right);
        delete_lv=findViewById(R.id.delete_lv);
        mdata=Dbmanager.allcityname();
        dcity=new ArrayList<>();
        delete_iv_error.setOnClickListener(this);
        delete_iv_right.setOnClickListener(this);
        deletecityAdapyer = new DeletecityAdapyer(this, mdata, dcity);
        delete_lv.setAdapter(deletecityAdapyer);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delete_iv_error:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示信息：").setMessage("确定舍弃更改？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               finish();
                            }
                        });
                builder.setNegativeButton("取消",null);
                builder.create().show();
                break;
            case R.id.delete_iv_right:
                for (int i = 0; i < dcity.size(); i++) {
                    String city=dcity.get(i);
                    //调用删除城市函数
                    Dbmanager.deletecity(city);
                }
                //删除成功后
                finish();
                break;
        }
    }
}
